package com.bjx.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjx.common.core.domain.R;
import com.bjx.common.core.domain.group.Update;
import com.bjx.system.domain.query.RoleMenuQuery;
import com.bjx.system.domain.query.RoleQuery;
import com.bjx.system.domain.vo.RoleMenuVO;
import com.bjx.system.domain.vo.RoleVO;
import com.bjx.system.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 角色控制器
 * @author: liushuai
 * @date: 2022/10/11
 */
@RequestMapping("/role")
@Tag(name = "角色控制器")
@RestController
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 分页查询角色列表
     *
     * @param roleQuery
     * @return: R<IPage < RoleVO>>
     * @Author: liushuai
     * @Date: 2022/10/11
     */
    @Operation(summary = "分页查询角色列表")
    @GetMapping("listRolePage")
    public R<IPage<RoleVO>> listRolePage(RoleQuery roleQuery) {
        IPage<RoleVO> page = roleService.listRolePage(roleQuery);
        return R.success(page);
    }

    /**
     * 查询所有的角色
     * @param
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.system.domain.vo.RoleVO>>
     * @Author: liushuai
     * @Date: 2022/10/17 17:00
     */
    @Operation(summary = "查询所有的角色")
    @GetMapping("listRoleAll")
    public R<List<RoleVO>> listRoleAll() {
        List<RoleVO> list = roleService.listRoleAll();
        return R.success(list);
    }

    /**
     * 根据角色id查询角色
     *
     * @param roleId
     * @return:R<RoleVO>
     * @Author: liushuai
     * @Date: 2022/10/11
     */
    @Operation(summary = "根据角色id查询角色")
    @GetMapping("selectRoleByRoleId")
    public R<RoleVO> selectRoleByRoleId(@RequestParam Long roleId) {
        RoleVO vo = roleService.selectRoleByRoleId(roleId);
        return R.success(vo);
    }

    /**
     * 根据角色id删除角色
     *
     * @param roleId
     * @return: R
     * @Author: liushuai
     * @Date: 2022/10/11
     */
    @Operation(summary = "根据角色id删除角色")
    @GetMapping("deleteRoleByRoleId")
    public R deleteRoleByRoleId(@RequestParam Long roleId) {
        roleService.deleteRoleByRoleId(roleId);
        return R.successMsg("删除角色成功");
    }

    /**
     * 新增角色
     *
     * @param roleVO
     * @return: R
     * @Author: liushuai
     * @Date: 2022/10/11
     */
    @Operation(summary = "新增角色")
    @PostMapping("insertRole")
    public R insertRole(@RequestBody @Validated RoleVO roleVO) {
        roleService.insertRole(roleVO);
        return R.successMsg("新增角色成功");
    }

    /**
     * 修改角色
     *
     * @param roleVO
     * @return: R
     * @Author: liushuai
     * @Date: 2022/10/11
     */
    @Operation(summary = "修改角色")
    @PostMapping("updateRole")
    public R updateRole(@RequestBody @Validated(Update.class) RoleVO roleVO) {
        roleService.updateRole(roleVO);
        return R.successMsg("修改角色成功");
    }


    /**
     * 根据角色Id查询角色权限关联
     *
     * @param roleId
     * @return: com.bjx.common.core.domain.R<com.bjx.system.domain.vo.RoleMenuVO>
     * @Author: liushuai
     * @Date: 2022/10/14 21:59
     */
    @Operation(summary = "根据角色Id查询角色权限关联")
    @GetMapping("selectRoleMenuVOByRoleId")
    public R<RoleMenuVO> selectRoleMenuVOByRoleId(@RequestParam Long roleId) {
        RoleMenuVO roleMenuVO = roleService.selectRoleMenuVOByRoleId(roleId);
        return R.success(roleMenuVO);
    }

    /**
     * 保存角色与菜单关联
     *
     * @param
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/10/14 22:55
     */
    @Operation(summary = "设置角色和权限关联")
    @PostMapping("saveRoleMenu")
    public R saveRoleMenu(@RequestBody RoleMenuQuery roleMenuQuery) {
        roleService.saveRoleMenu(roleMenuQuery);
        return R.successMsg("保存成功");
    }
}
